import * as api from '../../service/api'

const state = {
    userdata : {},
    company_code : '',
    fanslist : {},
    sellerlist : [],
    unsellerlist : [],
    mailList : [],
}

const getters = {

    userdata : state => state.userdata,

    company_code : state => state.company_code,

    fanslist : state => state.fanslist,

    sellerlist : state => state.sellerlist,

    unsellerlist : state => state.unsellerlist,

    mailList : state => state.mailList,
}

const actions = {

    //用户信息
    async getUserData ({state, commit}, para) {
        let res = await api.userData();
        if(res.errcode == 0){
            commit('setUserData', res.data);
            para.callback(res.data);
        }
    },

    //绑定粉丝
    async createFans ({commit, state}, para) {
        let res = await api.bindFans(para.openid);
        if(res.errcode == 0){
        }
    },

    //粉丝列表
    async fansList ({commit, state}, para) {
        let res = await api.getFansList(para.pageNo, para.pageSize);
        if(res.errcode == 0){
            commit('setFansList', res.data);
            para.callback(res.data);
        }
    },

    //申请分销商
    async applySeller ({commit, state}, para) {
        let res = await api.applySeller(para.user_base);
        if(res.errcode == 0){
            para.callback(res.data);
        }else{
            para.err(res.errmsg);
        }
    },

    //未审核分销商列表
    async unSellerList ({commit, state}, para) {
        let res = await api.unSellerList(para.nickname);
        if(res.errcode == 0){
            commit('setSellerList', res.data);
        }
    },

    //已审核分销商列表
    async sellerList ({commit, state}, para) {
        let res = await api.sellerList(para.nickname, para.level_code);
        if(res.errcode == 0){
            commit('setSellerList', res.data);
        }
    },

    //同意成为分销商
    async agreeSeller ({commit, state}, para) {
        let res = await api.agreeSeller(para.create_by, para.state);
        if(res.errcode == 0){
            para.callback();
        }
    },


    //创建邮寄信息
    async createMail ({commit, state}, para) {
        let res = await api.createMail(
            para.lxr_name,
            para.lxr_mobile,
            para.lxr_address,
            para.state,
        );
        if(res.errcode == 0){
            if(para && para.cb != undefined){
                para.cb(res);
            }
        }
    },


    //更新邮寄信息
    async updateMail ({commit, state}, para) {
        let res = await api.updateMail(
            para.id,
            para.lxr_name,
            para.lxr_mobile,
            para.lxr_address,
            para.state,
            para.del_flg,
        );
        if(res.errcode == 0){
            if(para && para.cb != undefined){
                para.cb(res);
            }
        }
    },

    //邮寄信息列表
    async getMailList ({commit, state}, para) {
        let res = await api.mailList(para.state);
        if(res.errcode == 0){
            commit('setMailList', res.data);
            if(para && para.cb != undefined) {
                para.cb(res.data);
            }
        }
    },

}

const mutations = {
    setUserData (state, value) {
        state.userdata = value;
    },

    setCompanyCode (state, value) {
        state.company_code = value;
    },

    setFansList (state, value) {
        state.fanslist = value;
    },

    setSellerList (state, value) {
        state.sellerlist = value;
    },

    setUnSellerList (state, value) {
        state.unsellerlist = value;
    },

    setMailList (state, value) {
        state.mailList = value;
    },
}

export default {
  state,
  getters,
  actions,
  mutations
}
